package net.hn.hnms.biz.energy.group.service.Impl;

import cn.hutool.core.util.StrUtil;
import net.hn.hnms.biz.energy.group.service.EnergyGroupService;
import net.hn.hnms.biz.energy.mapper.GroupStatisticsMapper;
import net.hn.hnms.biz.energy.request.GroupCommonReq;
import net.hn.hnms.biz.energy.vo.*;
import net.hn.hnms.sys.common.core.enums.CoalInfo;
import net.hn.hnms.biz.energy.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EnergyGroupServiceImpl implements EnergyGroupService {

    @Autowired
    private GroupStatisticsMapper groupStatisticsMapper;

    @Override
    public WaterSupplyGroupVo getWaterSupplyUnitCount(GroupCommonReq req) {

        //集团供水单位个数
        WaterSupplyGroupVo groupVo = groupStatisticsMapper.getWaterSupplyUnitCount(null, null, null).get(0);
        groupVo.setCompanyName("华能集团");
        //获取集团下的所有公司  编码,名称
//        Map<String, String> groupMap = CoalInfo.getAllGroup();
//        //所有公司编码
//        Set<String> set = groupMap.keySet();

        //获取每个公司的供水单位个数  companyInfo
        List<WaterSupplyGroupVo> companyInfo = groupStatisticsMapper.getWaterSupplyUnitCount(req.getCompanyCode(), null, "company");

        if (!StrUtil.isBlank(req.getCompanyName())) {
            companyInfo.stream().filter(c -> c.getCompanyName().contains(req.getCompanyName())).collect(Collectors.toList());
        }

        groupVo.setCompanyInfo(companyInfo);


        for (int g = 0; g < companyInfo.size(); g++) {
            //公司编码
            String companyCode = companyInfo.get(g).getCompanyCode();
            //加公司名字
            companyInfo.get(g).setCompanyName(CoalInfo.getCompanyNameByCompanyCode(companyCode));
            //该公司下所有煤矿编码
            String[] mineCodes = CoalInfo.getMineCodesByCompanyCode(companyCode);

            //存放公司下所有煤矿的信息
            List<WaterSupplyGroupVo> list = new ArrayList<>();

            for (int i = 0; i < mineCodes.length; i++) {
                ////获取每个煤矿的供水单位个数  mineInfo
                List<WaterSupplyGroupVo> mineInfo = groupStatisticsMapper.getWaterSupplyUnitCount(req.getCompanyCode(), mineCodes[i], "mine");
                for (WaterSupplyGroupVo s : mineInfo) {
                    s.setMineName(CoalInfo.getMineNameByMineCode(mineCodes[i]));
                    list.add(s);
                }
            }
            companyInfo.get(g).setMineInfo(list);
//
//            //循环该公司下所有煤矿
//            for (int i = 0; i < mineCodes.length; i++) {
//                //循环数据库查出来的煤矿信息
//                for (int j = 0; j < mineInfo.size(); j++) {
//                    //mineCode相等，则该煤矿是此公司下级单位
//                    if (mineCodes[i].equals(mineInfo.get(j).getMineCode())) {
//                        list.add(mineInfo.get(j));
//                    }
//                }
//            }
//            //将煤矿信息加到此公司信息中
//            companyInfo.get(g).setMineInfo(list);

        }
        groupVo.setWaterSupplyCompanyCount(groupVo.getWaterSupplyCompanyCount());
        return groupVo;
    }

    @Override
    public WaterPlacePriceGroupVo getUnitPriceOfWater(GroupCommonReq req) {
        //集团供水单位个数
        WaterPlacePriceGroupVo groupVo = groupStatisticsMapper.getUnitPriceOfWaterCount(null, null, null).get(0);
        groupVo.setCompanyName("华能集团");

        WaterSupplyGroupVo swgvList = getWaterSupplyUnitCount(req);

        groupVo.setWaterSupplyCompanyCount(swgvList.getWaterSupplyCompanyCount());

        //获取每个公司的供水单位个数  companyInfo
        List<WaterPlacePriceGroupVo> companyInfo = groupStatisticsMapper.getUnitPriceOfWaterCount(req.getCompanyCode(), null, "company");

        //每个公司的供水单位个数
        for (WaterPlacePriceGroupVo wpp : companyInfo) {
            for (WaterSupplyGroupVo wsg : swgvList.getCompanyInfo()) {
                if (wpp.getCompanyCode().equals(wsg.getCompanyCode())) {
                    wpp.setWaterSupplyCompanyCount(wsg.getWaterSupplyCompanyCount());
                }
            }
        }

        if (!StrUtil.isBlank(req.getCompanyName())) {
            companyInfo.stream().filter(c -> c.getCompanyName().contains(req.getCompanyName())).collect(Collectors.toList());
        }

        groupVo.setCompanyInfo(companyInfo);


        for (int g = 0; g < companyInfo.size(); g++) {
            //公司编码
            String companyCode = companyInfo.get(g).getCompanyCode();
            //加公司名字
            companyInfo.get(g).setCompanyName(CoalInfo.getCompanyNameByCompanyCode(companyCode));
            //该公司下所有煤矿编码
            String[] mineCodes = CoalInfo.getMineCodesByCompanyCode(companyCode);

            //存放公司下所有煤矿的信息
            List<WaterPlacePriceGroupVo> list = new ArrayList<>();

            for (int i = 0; i < mineCodes.length; i++) {
                //获取每个煤矿的用水单位个数  mineInfo
                List<WaterPlacePriceGroupVo> mineInfo = groupStatisticsMapper.getUnitPriceOfWaterCount(req.getCompanyCode(), mineCodes[i], "mine");
                for (WaterPlacePriceGroupVo s : mineInfo) {
                    s.setMineName(CoalInfo.getMineNameByMineCode(mineCodes[i]));
                    for (WaterSupplyGroupVo m : swgvList.getCompanyInfo()) {
                        for (WaterSupplyGroupVo ws : m.getMineInfo()) {
                            if (ws.getMineCode().equals(s.getMineCode())) {
                                s.setWaterSupplyCompanyCount(ws.getWaterSupplyCompanyCount());
                            }
                        }
                    }

                    list.add(s);
                }

            }
            companyInfo.get(g).setMineInfo(list);

        }
        groupVo.setWaterSupplyCompanyCount(groupVo.getWaterSupplyCompanyCount());
        return groupVo;

    }

    @Override
    public WaterSettlementGroupVo getWaterSettlement(GroupCommonReq req) {
        //集团供水单位个数
        WaterSettlementGroupVo groupVo = groupStatisticsMapper.getWaterSettlementCount(null, null, null).get(0);
        groupVo.setCompanyName("华能集团");

        WaterSupplyGroupVo swgvList = getWaterSupplyUnitCount(req);

        groupVo.setWaterSupplyCompanyCount(swgvList.getWaterSupplyCompanyCount());
        //总用水量
        List<WaterSettlementGroupVo> wsgList= groupStatisticsMapper.getWaterSettlementCount(null, null, null);
        if (wsgList!=null&&wsgList.size()!=0){
            groupVo.setWaterConsumptionCount(wsgList.get(0).getWaterConsumptionCount());
        }
        //获取每个公司的供水单位个数  companyInfo
        List<WaterSettlementGroupVo> companyInfo = groupStatisticsMapper.getWaterSettlementCount(req.getCompanyCode(), null, "company");

        //每个公司的供水单位个数
        for (WaterSettlementGroupVo wpp : companyInfo) {
            for (WaterSupplyGroupVo wsg : swgvList.getCompanyInfo()) {
                if (wpp.getCompanyCode().equals(wsg.getCompanyCode())) {
                    wpp.setWaterSupplyCompanyCount(wsg.getWaterSupplyCompanyCount());
                }
            }
        }

        if (!StrUtil.isBlank(req.getCompanyName())) {
            companyInfo.stream().filter(c -> c.getCompanyName().contains(req.getCompanyName())).collect(Collectors.toList());
        }

        groupVo.setCompanyInfo(companyInfo);


        for (int g = 0; g < companyInfo.size(); g++) {
            //公司编码
            String companyCode = companyInfo.get(g).getCompanyCode();
            //加公司名字
            companyInfo.get(g).setCompanyName(CoalInfo.getCompanyNameByCompanyCode(companyCode));
            //该公司下所有煤矿编码
            String[] mineCodes = CoalInfo.getMineCodesByCompanyCode(companyCode);

            //存放公司下所有煤矿的信息
            List<WaterSettlementGroupVo> list = new ArrayList<>();

            for (int i = 0; i < mineCodes.length; i++) {
                //获取每个煤矿的用水单位个数  mineInfo
                List<WaterSettlementGroupVo> mineInfo = groupStatisticsMapper.getWaterSettlementCount(req.getCompanyCode(), mineCodes[i], "mine");
                for (WaterSettlementGroupVo s : mineInfo) {
                    s.setMineName(CoalInfo.getMineNameByMineCode(mineCodes[i]));
                    for (WaterSupplyGroupVo m : swgvList.getCompanyInfo()) {
                        for (WaterSupplyGroupVo ws : m.getMineInfo()) {
                            if (ws.getMineCode().equals(s.getMineCode())) {
                                s.setWaterSupplyCompanyCount(ws.getWaterSupplyCompanyCount());
                            }
                        }
                    }

                    list.add(s);
                }

            }
            companyInfo.get(g).setMineInfo(list);

        }
        groupVo.setWaterSupplyCompanyCount(groupVo.getWaterSupplyCompanyCount());
        return groupVo;
    }


    @Override
    public List<WaterSupplyGroupVo> getNewWaterSupplyUnitCount(GroupCommonReq req) {
        List<WaterSupplyGroupVo> list=groupStatisticsMapper.getNewWaterSupplyUnitCount(req);
        list.forEach(l->{
            l.setCompanyName(CoalInfo.getCompanyNameByCompanyCode(l.getCompanyCode()));
        });
        return list;
    }

    @Override
    public List<WaterPlacePriceGroupVo> getNewUnitPriceOfWater(GroupCommonReq req) {
        List<WaterPlacePriceGroupVo> list = groupStatisticsMapper.getNewUnitPriceOfWater(req);
        list.forEach(l->{
            l.setCompanyName(CoalInfo.getCompanyNameByCompanyCode(l.getCompanyCode()));
        });
        return list;
    }

    @Override
    public List<WaterSettlementGroupVo> getNewWaterSettlement(GroupCommonReq req) {
        List<WaterSettlementGroupVo> list = groupStatisticsMapper.getNewWaterSettlement(req);
        list.forEach(l->{
            l.setCompanyName(CoalInfo.getCompanyNameByCompanyCode(l.getCompanyCode()));
        });
        return list;
    }

    @Override
    public List<WaterRecordGroupVo> getNewWaterAccount(GroupCommonReq req) {
        List<WaterRecordGroupVo> list = groupStatisticsMapper.getNewWaterAccount(req);
        list.forEach(l->{
            l.setCompanyName(CoalInfo.getCompanyNameByCompanyCode(l.getCompanyCode()));
        });
        return list;
    }

    @Override
    public List<ElectricityPlacePriceVo> getPowerLoad(GroupCommonReq req) {
        List<ElectricityPlacePriceVo> list = groupStatisticsMapper.getPowerLoad(req);
        list.forEach(l->{
            l.setCompanyName(CoalInfo.getCompanyNameByCompanyCode(l.getCompanyCode()));
        });
        return list;
    }

    @Override
    public List<ElectricitySettlementGroupVo> getElectricityConsumptionStatistics(GroupCommonReq req) {
        List<ElectricitySettlementGroupVo> list = groupStatisticsMapper.getElectricityConsumptionStatistics(req);
        list.forEach(l->{
            l.setCompanyName(CoalInfo.getCompanyNameByCompanyCode(l.getCompanyCode()));
        });
        return list;
    }
}
